unit RegistryAction;

interface

uses Registry,
    Variants,
    Windows;

function LoadRegistry(APath, AKey: string; ARoot: HKEY = HKEY_CURRENT_USER): Variant;
function SaveRegistry(APath, AKey: string; AValue: Variant; ARoot: HKEY = HKEY_CURRENT_USER): Boolean;
function DeleteRegistry(APath, AKey: string; ARoot: HKEY = HKEY_CURRENT_USER): Boolean;
function LoadRegistryBinary(APath, AKey: string; ARoot: HKEY = HKEY_CURRENT_USER): Variant;
function SaveRegistryBinary(APath, AKey: string; AValue: Variant; ARoot: HKEY = HKEY_CURRENT_USER): Boolean;

implementation

function LoadRegistry(APath, AKey: string; ARoot: HKEY = HKEY_CURRENT_USER): Variant;
var Registry: TRegistry;
    Ret: Variant;
begin
    Ret := null;
    Registry := TRegistry.Create;
    Registry.RootKey := ARoot;
    if Registry.OpenKeyReadOnly(APath) then
        Ret := Registry.Readstring(AKey);
    Registry.CloseKey;
    Registry.Free;
    if VarIsNull(Ret) then
        LoadRegistry := ''
    else
        LoadRegistry := Ret;
end;

function SaveRegistry(APath, AKey: string; AValue: Variant; ARoot: HKEY = HKEY_CURRENT_USER): Boolean;
var Registry: TRegistry;
    Ret: Boolean;
begin
    Ret := False;
    Registry := TRegistry.Create;
    Registry.RootKey := ARoot;
    if Registry.OpenKey(APath, True) then try
        Registry.Writestring(AKey, AValue);
        Ret := True;
    except
        Ret := False;
    end;
    Registry.CloseKey;
    Registry.Free;
    SaveRegistry := Ret;
end;

function LoadRegistryBinary(APath, AKey: string; ARoot: HKEY = HKEY_CURRENT_USER): Variant;
var Registry: TRegistry;
    Ret: Variant;
    buf: string;
begin
    Ret := null;
    Registry := TRegistry.Create;
    try
        Registry.RootKey := ARoot;
        if Registry.OpenKeyReadOnly(APath) then
            Ret := Registry.ReadBinaryData(AKey, buf, SizeOf(500));
        Registry.CloseKey;
    finally
        Registry.Free;
    end;
    LoadRegistryBinary := Ret;
end;

function SaveRegistryBinary(APath, AKey: string; AValue: Variant; ARoot: HKEY = HKEY_CURRENT_USER): Boolean;
var Registry: TRegistry;
    Ret: Boolean;
begin
    Ret := False;
    Registry := TRegistry.Create;
    try
        Registry.RootKey := ARoot;
        if Registry.OpenKey(APath, True) then try
            Registry.WriteBinaryData(AKey, AValue, SizeOf(500));
            Ret := True;
        except
            Ret := False;
        end;
        Registry.CloseKey;
    finally
        Registry.Free;
    end;
    SaveRegistryBinary := Ret;
end;

function DeleteRegistry(APath, AKey: string; ARoot: HKEY = HKEY_CURRENT_USER): Boolean;
var Registry: TRegistry;
begin
    Registry := TRegistry.Create;
    Registry.RootKey := ARoot;
    Registry.OpenKey(APath, True);
    DeleteRegistry := Registry.DeleteValue(AKey);
    Registry.CloseKey;
    Registry.Free;
end;

end.

